package edu.mju.scenic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import edu.mju.scenic.dto.AllSpotDto;
import edu.mju.scenic.dto.ScenicSpotDto;
import edu.mju.scenic.entity.Img;
import edu.mju.scenic.entity.Spot;
import edu.mju.scenic.mapper.SpotMapper;
import edu.mju.scenic.service.AttractionsService;
import edu.mju.scenic.service.CelebrityService;
import edu.mju.scenic.service.ImgService;
import edu.mju.scenic.service.SpotService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.mju.scenic.vo.IndexSpotVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CBL
 * @since 2020-12-28
 */
@Service
public class SpotServiceImpl extends ServiceImpl<SpotMapper, Spot> implements SpotService {

    @Autowired
    private ImgService imgService;

    @Autowired
    private AttractionsService attractionsService;

    @Autowired
    private CelebrityService celebrityService;

    @Override
    public Map<String, Object> getSpotList(IPage<Spot> spotPage, ScenicSpotDto scenicSpotDto) {
        //前端传入条件转换Spot对象
        Spot searchSpot = new Spot();
        BeanUtils.copyProperties(scenicSpotDto,searchSpot);

        QueryWrapper<Spot> wrapper = new QueryWrapper<>();

        if(StringUtils.isNotEmpty(searchSpot.getAddr())){
            wrapper.like("addr",searchSpot.getAddr());
        }
        if(StringUtils.isNotEmpty(searchSpot.getTitle())){
            wrapper.like("title",searchSpot.getTitle());
        }
        if(searchSpot.getPrice()!=null){
            wrapper.le("price",searchSpot.getPrice());
        }
        if(searchSpot.getStatus()!=null && searchSpot.getStatus() != 0){
            wrapper.eq("status",searchSpot.getStatus());
        }

        this.page(spotPage,wrapper);

        //取出需要的结果
        long total = spotPage.getTotal();

        List<Spot> records = spotPage.getRecords();

        List<IndexSpotVo> indexSpotVoList = Lists.newArrayList();

        //把结果封装到indexSpotVoList中
        for (Spot spot : records) {
            //根据id查询图片
            IndexSpotVo indexSpotVo = new IndexSpotVo();
            QueryWrapper<Img> imgQueryWrapper = new QueryWrapper<>();
            imgQueryWrapper.eq("spot_id",spot.getId());
            List<Img> list = imgService.list(imgQueryWrapper);
            indexSpotVo.setSpotImg(list);
            BeanUtils.copyProperties(spot,indexSpotVo);
            indexSpotVoList.add(indexSpotVo);
        }

        Map<String,Object> map = Maps.newHashMap();
        map.put("total",total);
        map.put("data",indexSpotVoList);

        return map;
    }

    @Override
    public void saveOrUpdateSpot(IndexSpotVo indexSpotVo) {
        if(indexSpotVo.getId()!=null){
            //更新操作

            //更新景点
            Spot spot = new Spot();
            BeanUtils.copyProperties(indexSpotVo,spot);
            baseMapper.updateById(spot);
            if(!CollectionUtils.isEmpty(indexSpotVo.getSpotImg())){

                List<Img> spotImg = indexSpotVo.getSpotImg();
                //更新图片
                for (Img img : spotImg) {
                    if(img.getId()!=null){
                        //更新图片
                        imgService.updateById(img);
                    }else {
                        //新增图片
                        img.setSpotId(spot.getId());
                        imgService.save(img);
                    }
                }
            }
        }else {
            //新增操作
            //新增景点
            Spot spot = new Spot();
            BeanUtils.copyProperties(indexSpotVo,spot);
            //添加成功 返回自增id
            boolean save = this.save(spot);

            //新增景点图片
            List<Img> spotImg = indexSpotVo.getSpotImg();
            if(!CollectionUtils.isEmpty(spotImg)){
                for (Img img : spotImg) {
                    img.setSpotId(spot.getId());
                    imgService.save(img);
                }
            }


        }
    }

    @Override
    public int deleteScenicSpot(Integer id) {
        //删除名人
        celebrityService.deleteCelebrityBySpotId(id);
        //删除名胜
        attractionsService.deleteBySpotId(id);
        //删除img
        imgService.deleteBySpotId(id);
        //删除景点
        boolean spot = baseMapper.deleteBySpotId(id);
        if(spot){
            return 1;
        }
        return 0;
    }

    @Override
    public Map<String, Object> getScenicSpotAllInfo(Integer id) {
        List<AllSpotDto> list = baseMapper.getScenicSpotAllInfo(id);
        Map<String,Object> map = new HashMap<>();
        map.put("data",list);
        return map;
    }
}
